use crate::{
    models::user::{User, UserModel},
    types::response::ApiResponse,
};
use sea_orm::DbErr;
use tracing::{info, instrument};

/// 用户服务结构体
#[derive(Clone)]
pub struct UserService {
    user_model: UserModel,
}

impl UserService {
    pub fn new(user_model: UserModel) -> Self {
        Self { user_model }
    }

    /// 获取用户列表（分页）
    #[instrument(skip(self))]
    pub async fn list_users(
        &self,
        page: u64,
        page_size: u64,
    ) -> Result<Vec<User>, DbErr> {
        info!("获取用户列表: 第{}页，每页{}条", page, page_size);
        self.user_model.find_all(page, page_size).await
    }

    /// 获取单个用户详情
    #[instrument(skip(self))]
    pub async fn get_user(&self, user_id: i32) -> Result<Option<User>, DbErr> {
        info!("获取用户详情: ID={}", user_id);
        self.user_model.find_by_id(user_id).await
    }

    /// 更新用户信息
    #[instrument(skip(self))]
    pub async fn update_user(
        &self,
        user_id: i32,
        username: Option<&str>,
        email: Option<&str>,
    ) -> Result<User, DbErr> {
        info!("更新用户信息: ID={}", user_id);
        self.user_model.update_user(user_id, username, email).await
    }

    /// 删除用户（软删除）
    #[instrument(skip(self))]
    pub async fn delete_user(&self, user_id: i32) -> Result<u64, DbErr> {
        info!("删除用户: ID={}", user_id);
        self.user_model.soft_delete(user_id).await
    }
}